package com.nbjtjc.safe.api.sys;

import com.nbjtjc.safe.bean.Page;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.model.sys.SysNotice;
import com.nbjtjc.safe.service.sys.SysNoticeService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 系统通知
 */
@RequestMapping("/sys/notice")
@RestController
@CrossOrigin("*")
public class SysNoticeController {
    @Autowired
    private SysNoticeService sysNoticeService;

    @RequestMapping(value="/update",method = {RequestMethod.POST})
    public Result update(SysNotice sysNotice){
        Result result=new Result<>();
        try {
            result=sysNoticeService.update(sysNotice);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse("插入失败");
            return  result;
        }
        return result;
    }



    @RequestMapping(value="/add",method = {RequestMethod.POST})
    public Result add(SysNotice sysNotice){
        Result result=new Result<>();
        try {
            result.setData(sysNoticeService.add(sysNotice));
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse("插入失败");
            return  result;
        }
        return result;
    }

    @RequestMapping(value="/delete",method = {RequestMethod.POST})
    public Result delete(SysNotice sysNotice){
        Result result=new Result<>();
        try {
            result=sysNoticeService.delete(sysNotice);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse("删除失败："+e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/find" ,method = {RequestMethod.GET,RequestMethod.POST})
    public Result find(SysNotice sysNotice,Page page){
        Result result=new Result();
        try {
            result=sysNoticeService.find(sysNotice,page);
        } catch (Exception e) {
            e.printStackTrace();
           result.setFalse(e.getMessage());
        }
        return result;
    }

    @GetMapping(value = "/getMyNotice" )
    public Result getMyNotice(Boolean hasRead,Page page){
        Result result=new Result();
        try {
            result.setData(sysNoticeService.getMyNotice(hasRead,page));
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @GetMapping(value = "/getUnreadCount")
    public Result getUnreadCount(){
        Result result=new Result();
        result.setData(sysNoticeService.getUnReadNoticeCount());
        return result;
    }

    @PostMapping(value = "/readNotice" )
    public Result readNotice(@Param("sysNoticeTargetId") int sysNoticeTargetId
            ,@Param("noticeId") int noticeId){

        Result result=new Result();
        try {
            result.setData(sysNoticeService.readNotice(sysNoticeTargetId,noticeId));
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }
}
